package org.bp.ioc.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
 * Created by bp on 18/03/07.
 */
public final class PropUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(PropUtils.class);

    public static  Properties loadProps(String fileName){
        Properties prpo = null;
        InputStream in = null;
        try {
            in = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
            prpo = new Properties();
            prpo.load(in);
        }catch (IOException e){
            LOGGER.error("load file failed",e);
        }finally {
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    LOGGER.error("close inputStream failed",e);
                }
            }
        }


        return prpo;
    }

    public static String getString(Properties props,String key,String defaultVal){
        if(props.contains(key)){
            return  props.getProperty(key);
        }
        return defaultVal;
    }

    public static String getString(Properties props,String key){
        return getString(props,key,"");
    }
}
